package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.mapper.CourseMapper;
import com.example.mapper.RequestMapper;
import com.example.pojo.Admin;
import com.example.mapper.AdminMapper;
import com.example.pojo.Course;
import com.example.pojo.Request;
import com.example.pojo.Student;
import com.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @ author:lin tingfeng
 * @ data:2021/9/11-21:14
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private RequestMapper requestMapper;

    /******************
     ******登录验证******
     ******************/
    @Override
    public Boolean validateLogin(String account, String password) {
        Admin admin = adminMapper.selectById(account);
        if (admin != null && admin.getPassword().equals(password)) {
            return true;
        }
        return false;
    }
/************************************************************************************/
/************************************************************************************/

    /******************
     ******成绩方法******
     ******************/
    @Override
    public List<Course> selectCourseByAll() {
        return courseMapper.selectList(null);
    }

    @Override
    public List<Course> selectCourseById(String stuid) {
        List<Course> list_All = selectCourseByAll();
        List<Course> list = new ArrayList<>();
        for (Course course : list_All) {
            if (course.getStuid().equals(stuid))
                list.add(course);
        }
        return list;
    }

    @Override
    public Course selectCourseById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public Boolean selectCourseByEntity(Course course) {
        List<Course> list = selectCourseById(course.getStuid());
        if (list != null) {
            for (Course c : list) {
                if (c.getCourse().equals(course.getCourse()))
                    return false;
            }
        }
        return true;
    }

    @Override
    public List<Course> selectCourseByType(String type, String content) {
        //条件封装
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (type.equals("stuid")) {
            queryWrapper.like(StringUtils.isNotBlank(content), "stuid", content);
            return courseMapper.selectList(queryWrapper);
//        } else if (type.equals("name")) {
//            queryWrapper.like(StringUtils.isNotBlank(content), "name", content);
//            return courseMapper.selectList(queryWrapper);
        } else {
            queryWrapper.like(StringUtils.isNotBlank(content), "course", content);
            return courseMapper.selectList(queryWrapper);
        }
    }

    @Override
    public Boolean deleteCourseById(Integer id) {
        if (courseMapper.deleteById(id) != 0)
            return true;
        return false;
    }

    @Override
    public Boolean updateCourse(Course course) {
        if (courseMapper.updateById(course) != 0)
            return true;
        return false;
    }

    @Override
    public Boolean insertCourse(Course course) {
        if (selectCourseByEntity(course) && courseMapper.insert(course) != 0)
            return true;
        return false;
    }
    //    @Override
//    public Boolean deleteCourse(Course course) {
//        List<Course> list = selectCourseById(course.getStuid());
//        if (list != null) {
//            QueryWrapper<Course> wrapper = new QueryWrapper<>();
//            wrapper.eq("course", course.getCourse());
//            wrapper.eq("stuid", course.getStuid());
//            if (courseMapper.delete(wrapper) != 0)
//            for (Course c:list
//                 ) {
//                if (c.getStuid().equals(course.getStuid())&&c.getCourse().equals(course.getCourse()))
//
//            }
//        }
//        return false;
/************************************************************************************/
/************************************************************************************/

    /******************
     ******审核方法******
     ******************/
    @Override
    public List<Request> selectRequestByAll() {
        return requestMapper.selectList(null);
    }

    @Override
    public Boolean updateRequestById(Integer id, String status) {
        Request request = selectRequestById(id);
        if (request != null) {
            request.setStatus(status);
            if (requestMapper.updateById(request) != 0)
                return true;
        }
        return false;
    }

    @Override
    public Request selectRequestById(Integer id) {
        return requestMapper.selectById(id);
    }

    @Override
    public Boolean insertRequest(Request request) {
        if (requestMapper.insert(request) != 0)
            return true;
        return false;
    }
/************************************************************************************/
/************************************************************************************/
}
